package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.enums.AuditStatus;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.DingDanDetailMapper;
import com.ruoyi.system.mapper.DingDanMapper;
import com.ruoyi.system.mapper.HuoPinMapper;
import com.ruoyi.system.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 订单信息 服务实现类
 * </p>
 *
 * @author qubaofeng
 * @since 2025-02-16
 */
@Service
public class DingDanServiceImpl extends ServiceImpl<DingDanMapper, DingDan> implements IDingDanService {

    @Resource
    private IDingDanDetailService dingDanDetailService;

    @Resource
    private DingDanDetailMapper dingDanDetailMapper;

    @Resource
    private HuoPinMapper huoPinMapper;

    @Resource
    private IDangkouService dangkouService;

    @Resource
    private ICaiGouDanService caiGouDanService;

    @Resource
    private ICangKuService cangKuService;

    @Override
    public List<DangKou> selectDingDanList(DingDan user) {
        return this.baseMapper.selectDingDanList(user);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveDingDan(DingDan dingDan) {
        dingDan.setCangKuId(dingDan.getList().get(0).getCangKuId());

        if(dingDan.getId() == null){
            dingDan.setStatus(0);
            baseMapper.insert(dingDan);
            dingDanDetailService.saveList(dingDan);
        } else {
            updateDingDanInfo(dingDan);
        }

        return true;
    }

    private void updateDingDanInfo(DingDan dingDan) {
        baseMapper.updateById(dingDan);
        dingDanDetailService.deleteDetail(dingDan);
        dingDanDetailService.saveList(dingDan);
    }

    @Override
    public DingDan getDetail(Long id) {
        DingDan dingDan = baseMapper.selectDingDanById(id);
        if(dingDan != null){
            LambdaQueryWrapper<DingDanDetail> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(DingDanDetail::getDingDanId, id);
            List<DingDanDetail> details = dingDanDetailMapper.selectList(wrapper);
            for (DingDanDetail detail : details) {
                detail.setHuoPin(huoPinMapper.selectById(detail.getHuoPinId()));
            }
            dingDan.setList(details);
        }
        return dingDan;
    }

    @Override
    public List<DingDan> listByUserId(Long userId) {
        List<DangKou> dangKous = dangkouService.selectByAdmin(userId);

        List<Long> dangkouIds = dangKous.stream().map(DangKou::getId).collect(Collectors.toList());

        List<DingDan> dingDanList = baseMapper.selectListByDangKouIds(dangkouIds);

        for(DingDan dingDan : dingDanList) {
            dingDan.setList(dingDanDetailService.selectByDingDan(dingDan.getId()));
        }
        return dingDanList;
    }


    @Override
    public List<DingDanDetail> getDetailByRiQiAndCaiPin(String riQi, List<Long> huoPinId) {
        return dingDanDetailMapper.getDetailByRiQiAndCaiPin(riQi, huoPinId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public synchronized boolean  shehe(DingDan dingDan) {
        DingDan old = baseMapper.selectById(dingDan.getId());
        // 只在第一次审核通过时生成采购单
        if(!AuditStatus.PASS.getCode().equals(old.getStatus())){
            //审核通过时，自动生成采购单
            if(AuditStatus.PASS.getCode().equals(dingDan.getStatus())){
                createCaiGouDan(dingDan);
            }
        }
        baseMapper.updateById(dingDan);

        return true;
    }

    private void createCaiGouDan(DingDan dingDan) {
        List<DingDanDetail> details = dingDanDetailService.selectByDingDan(dingDan.getId());

        for(DingDanDetail detail : details){
            CaiGouDan caiGouDan = new CaiGouDan();
            caiGouDan.setRiQi(detail.getRiQi());
            caiGouDan.setShiJian(detail.getShiJian());
            caiGouDan.setBuyCount(detail.getBuyCount());
            caiGouDan.setDingDanId(dingDan.getId());
            caiGouDan.setDingDanDetailId(detail.getId());
            caiGouDan.setHuoPinId(detail.getHuoPinId());
            caiGouDan.setCreateTime(new Date());
            caiGouDan.setCreateBy(dingDan.getUpdateBy());

            caiGouDanService.save(caiGouDan);
        }
    }

    @Override
    public List<DingDan> listDingDanHistory(Long userId, Long huoPinId, String timeStart, String timeEnd) {
        if(StringUtils.isNotBlank(timeStart)){
            timeStart = timeStart+" 00:00:00";
        }
        if(StringUtils.isNotBlank(timeEnd)){
            timeEnd = timeEnd +" 23:59:59";
        }
        List<DangKou> dangKous = dangkouService.selectByAdmin(userId);

        List<Long> dangkouIds = dangKous.stream().map(DangKou::getId).collect(Collectors.toList());

        List<DingDan> dingDanList = baseMapper.selectHistoryList(dangkouIds,huoPinId, timeStart, timeEnd);

        for(DingDan dingDan : dingDanList) {
            dingDan.setList(dingDanDetailService.selectByDingDan(dingDan.getId()));
        }
        return dingDanList;
    }
}
